/*
 *  Copyright 2010 mazhao.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package shopping.management;

import com.opensymphony.xwork2.ModelDriven;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import shopping.commons.BaseAction;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.model.Catalog;


/**
 * @author mazhao
 */
public class CatalogAction extends BaseAction implements ModelDriven<Catalog> {
    private Log log = LogFactory.getLog(this.getClass());

    // model object
    private Catalog catalog = new Catalog();

    private List<Catalog> allCatalog = new ArrayList<Catalog>();

    /**
     * list all catalog for creating, updating or deleting.
     *
     * @return catalog listing page
     */
    @Action(
            value = "/management/cataloglist",
            results = {
                    @Result(name = "success", location = "/WEB-INF/content/management/catalog.jsp")
            }
    )
    public String list() {
        if (log.isDebugEnabled()) {
            log.debug("enter management/cataloglist action");
        }

        // select add catalog without tree layout.
        allCatalog = catalog.selectCatalog();

        log.debug("catalog size:" + allCatalog.size());
        for (Catalog c : allCatalog) {
            log.debug(c);
        }
        return SUCCESS;
    }

    /**
     * delete a existing catalog.
     *
     * @return forward to list page
     */
    @Action(
            value = "/management/catalogdelete",
            results = {
                    @Result(name = "success", location = "/management/cataloglist", type = "redirect")
            }
    )
    public String delete() {
        log.debug("enter catalog delete");

        // load data from primary key id(lft and rgt are expected )
        getModel().load();
        // delete catalog and update other lft and rgt
        getModel().delete();

        return SUCCESS;
    }

    /**
     * update catalog name and description
     * @return catalog list page
     */
    @Action(
            value = "/management/catalogupdate",
            results = {
                    @Result(name = "success", location = "/management/cataloglist", type = "redirect")
            }
    )
    public String update() {
        log.debug("enter catalog update");

        getModel().update();

        return SUCCESS;
    }

    /**
     * create new catalog
     * @return catalog list page
     */
    @Action(
            value = "/management/catalogcreate",
            results = {
                    @Result(name = "success", location = "/management/cataloglist", type = "redirect")
            }
    )
    public String create() {
        log.debug("enter catalog create:" + getModel());

        Catalog nc = new Catalog();
        nc.setName(getModel().getName());
        nc.setDescription(getModel().getDescription());

        // load parent catalog
        getModel().load();

        nc.setLft(getModel().getRgt());
        nc.setRgt(getModel().getRgt() + 1);
        nc.save();

        return SUCCESS;
    }

    //===================================================
    // ModelDriven method
    //===================================================

    public Catalog getModel() {
        return catalog;
    }

    // ---- expose to page ----
    public List<Catalog> getAllCatalog() {
        return allCatalog;
    }
}
